﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using TaskManagement.ModelLayer;
using TaskManagement.BusinessLayer;

namespace TaskManagement.WcfServiceLibrary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class TaskService : ITaskService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public IList<Task> SearchTasks(Task t)
        {
            TaskControler tc = new TaskControler();
            return tc.SearchTask(t);
        }

        public IList<Task> SearchTasksRest(int id, string description, string account, string delivery, string tag, DateTime date, string status, int estimate, int timespent, int employeeid)
        {
            Task t = CreateTaskFromData(date, description, account, delivery, tag, status, estimate, timespent, id, employeeid);
            TaskControler tc = new TaskControler();
            return tc.SearchTask(t); ;
        }

        public int CreateTask(Task task)
        {
            TaskControler tc = new TaskControler();
            return tc.CreateTask(task);
        }

        public int CreateTaskRest(string description, string account, string delivery, string tag, DateTime date, string status, int estimate, int timespent)
        {
            Task t = CreateTaskFromData(date, description, account, delivery, tag, status, estimate, timespent);
            TaskControler tc = new TaskControler();
            return tc.CreateTask(t);
        }

        public int DeleteTasks(IList<Task> tasks)
        {
            TaskControler tc = new TaskControler();
            return tc.DeleteTasks(tasks);
        }

        public int DeleteTaskRest(int id)
        {
            TaskControler tc = new TaskControler();
            Task t = new Task();
            t.Id = id;
            List<Task> tasks = new List<Task>(1);
            tasks.Add(t);
            return tc.DeleteTasks(tasks);
        }

        private Task CreateTaskFromData(DateTime date, string description = null, string account = null, string delivery = null, string tag = null, string status = null, int estimate = 0, int timespent = 0,int id = 0, int employeeid = 0 )
        {
            Task t = new Task();
            if (id != 0)
                t.Id = id;

            if (description != null)
                t.Description = description;

            if (account != null)
                t.Account = account;

            if (delivery != null)
                t.Delivery = delivery;

            if (tag != null)
                t.Tag = tag;

            if (date != DateTime.MinValue)
                t.Date = date;

            if (status != null)
                t.Status = status;

            if (estimate != 0)
                t.Estiamte = estimate - 1;

            if (timespent != 0)
                t.TimeSpent = timespent - 1;

            if (employeeid != 0)
            {
                Employee e = new Employee();
                e.Id = employeeid - 1;
                t.Employee = e;
            }

            return t;
        }

        public int UpdateTask(Task task)
        {
            TaskControler tc = new TaskControler();
            return tc.UpdateTask(task);
        }

        public int UpdateTaskRest(int id, string description, string account, string delivery, string tag, DateTime date, string status, int estimate, int timespent)
        {
            TaskControler tc = new TaskControler();
            Task task = CreateTaskFromData(date, description, account, delivery, tag, status, estimate, timespent, id);
            return tc.UpdateTask(task);
        }
    }
}
